package sk.stuba.fiit.repka.dp.owl.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;

import sk.stuba.fiit.repka.dp.owl.MyOWLClass;
import sk.stuba.fiit.repka.dp.owl.MyOWLObjectProperty;
import sk.stuba.fiit.repka.dp.owl.OWLRestriction;

public class CreateRestrictionDialog extends JDialog implements ActionListener{

	private final JPanel contentPanel = new JPanel();
	private JComboBox chooseObjCB;
	private JComboBox chooseClassCB;
	private ButtonGroup group;
	
	private List<MyOWLObjectProperty> objProperties; //input
	private List<MyOWLClass> classes;// input
	
	private String outputObjProperty; //output
	private String outputClass; //output
	private String outpuValue; //output
	
	public String getOutputObjProperty() {
		return outputObjProperty;
	}

	public String getOutputClass() {
		return outputClass;
	}

	public String getOutpuValue() {
		return outpuValue;
	}

	/**
	 * Create the dialog.
	 */
	public CreateRestrictionDialog(List<MyOWLObjectProperty> properties,  List<MyOWLClass> classes) {
		this.objProperties = new ArrayList<MyOWLObjectProperty>(properties);
		this.classes = new ArrayList<MyOWLClass>(classes);
		this.setModal(true);
		setTitle("Restriction");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[]{0, 0};
		gbl_contentPanel.rowHeights = new int[]{176, 0, 0};
		gbl_contentPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_contentPanel.rowWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
		contentPanel.setLayout(gbl_contentPanel);
		{
			JPanel comboPanel = new JPanel();
			GridBagConstraints gbc_comboPanel = new GridBagConstraints();
			gbc_comboPanel.insets = new Insets(0, 0, 5, 0);
			gbc_comboPanel.fill = GridBagConstraints.BOTH;
			gbc_comboPanel.gridx = 0;
			gbc_comboPanel.gridy = 0;
			contentPanel.add(comboPanel, gbc_comboPanel);
			GridBagLayout gbl_comboPanel = new GridBagLayout();
			gbl_comboPanel.columnWidths = new int[]{109, 207, 28, 0};
			gbl_comboPanel.rowHeights = new int[]{20, 0, 0, 0, 0};
			gbl_comboPanel.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
			gbl_comboPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
			comboPanel.setLayout(gbl_comboPanel);
			{
				JLabel chooseObjLB = new JLabel("Choose Object Property");
				GridBagConstraints gbc_chooseObjLB = new GridBagConstraints();
				gbc_chooseObjLB.insets = new Insets(20, 0, 5, 5);
				gbc_chooseObjLB.gridx = 1;
				gbc_chooseObjLB.gridy = 0;
				comboPanel.add(chooseObjLB, gbc_chooseObjLB);
			}
			{
				chooseObjCB = new JComboBox(this.initializeChooseObjCB(this.objProperties));
				GridBagConstraints gbc_chooseObjCB = new GridBagConstraints();
				gbc_chooseObjCB.fill = GridBagConstraints.HORIZONTAL;
				gbc_chooseObjCB.insets = new Insets(0, 0, 20, 5);
				gbc_chooseObjCB.gridx = 1;
				gbc_chooseObjCB.gridy = 1;
				comboPanel.add(chooseObjCB, gbc_chooseObjCB);
			}
			{
				JLabel chooseClassLB = new JLabel("Choose Class");
				GridBagConstraints gbc_chooseClassLB = new GridBagConstraints();
				gbc_chooseClassLB.insets = new Insets(0, 0, 5, 5);
				gbc_chooseClassLB.gridx = 1;
				gbc_chooseClassLB.gridy = 2;
				comboPanel.add(chooseClassLB, gbc_chooseClassLB);
			}
			{
				chooseClassCB = new JComboBox(this.initializeChooseClassCB(this.classes));
				GridBagConstraints gbc_chooseClassCB = new GridBagConstraints();
				gbc_chooseClassCB.insets = new Insets(0, 0, 0, 5);
				gbc_chooseClassCB.fill = GridBagConstraints.HORIZONTAL;
				gbc_chooseClassCB.gridx = 1;
				gbc_chooseClassCB.gridy = 3;
				comboPanel.add(chooseClassCB, gbc_chooseClassCB);
			}
		}
		{
			JPanel radioPanel = new JPanel();
			GridBagConstraints gbc_radioPanel = new GridBagConstraints();
			gbc_radioPanel.fill = GridBagConstraints.BOTH;
			gbc_radioPanel.gridx = 0;
			gbc_radioPanel.gridy = 1;
			contentPanel.add(radioPanel, gbc_radioPanel);
			JRadioButton allValuesRB = new JRadioButton("all values", true);
			radioPanel.add(allValuesRB);
			JRadioButton someValuesRB = new JRadioButton("some values");
			radioPanel.add(someValuesRB);
			group = new ButtonGroup();
			group.add(allValuesRB);
			group.add(someValuesRB);
			
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.addActionListener(this);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener(this);
				buttonPane.add(cancelButton);
			}
		}
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setVisible(true);
	}
	
	private String[] initializeChooseObjCB(List<MyOWLObjectProperty> inputCombo) {
		String[] result = new String[inputCombo.size()];
		int iter = 0;
		for(MyOWLObjectProperty o : inputCombo) {
			result[iter++] = o.getName();
		}
		return result;
	}
	
	private String[] initializeChooseClassCB(List<MyOWLClass> inputCombo) {
		String[] result = new String[inputCombo.size()];
		int iter = 0;
		for(MyOWLClass o : inputCombo) {
			result[iter++] = o.getName();
		}
		return result;
	}
	
	private JRadioButton getSelection(ButtonGroup group) {
	    for (Enumeration e=group.getElements(); e.hasMoreElements(); ) {
	        JRadioButton b = (JRadioButton)e.nextElement();
	        if (b.getModel() == group.getSelection()) {
	            return b;
	        }
	    }
	    return null;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if("OK".equals(e.getActionCommand())) {
			this.outputClass = (String)this.chooseClassCB.getSelectedItem();
			this.outputObjProperty = (String) this.chooseObjCB.getSelectedItem();
			JRadioButton selectedRadioBT = getSelection(this.group);
			if("all values".equals(selectedRadioBT.getText())) {
				this.outpuValue = OWLRestriction.USE_ALL_VALUES;
			} else  if("some values".equals(selectedRadioBT.getText())) {
				this.outpuValue = OWLRestriction.USE_SOME_VALUES;
			}
			
			this.dispose();
		} else if("Cancel".equals(e.getActionCommand())) {
			this.dispose();
		}
	}

}
